package KAs;//“解密”改成自己定义的包名就可以运行

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;


public class Main  extends JFrame implements ActionListener {
    char []a;

    JFrame jf2=new JFrame();

    String ys="";
    String yc="";
    boolean x1=true;
    boolean x2=false;
    JTextField jte=new JTextField(30);
    JTextField jte1=new JTextField(30);
    Font ft=new Font("楷体_GB2312",Font.BOLD,25);

    JButton jbt1=new JButton("加密");
    JButton jbt2=new JButton("解密");
    JButton jbt3=new JButton("查看加密文件");
    JButton jbt4=new JButton("查看解密文件");

    JPanel jp1=new JPanel();
    JPanel jp2=new JPanel();

    private Object 加密;
    public Main(){

        jf2.setBounds(340, 240, 700, 700);

        jf2.add(jte1);
        jf2.setVisible(false);
        //	jf2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jte.setFont(ft);
        jp2.add(jte);
        jp1.add(jbt1);
        jp1.add(jbt2);
        jp1.add(jbt3);
        jp1.add(jbt4);
        jbt3.setVisible(false);
        jbt4.setVisible(false);

        jbt1.setFont(ft);
        jbt2.setFont(ft);
        jbt3.setFont(ft);
        jbt4.setFont(ft);

        jp1.setBackground(Color.cyan);
        jbt1.setBackground(Color.orange);
        jbt2.setBackground(Color.green);
        jbt4.setBackground(Color.pink);

        jbt1.addActionListener(this);
        jbt2.addActionListener(this);
        jbt3.addActionListener(this);
        jbt4.addActionListener(this);
        this.setTitle("加解密");

        this.setBounds(200, 200, 700, 700);
        this.add(jp2,"North");
        this.add(jp1);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout g1=new GridLayout(2,2);
        g1.setHgap(20);
        g1.setVgap(20);
        jp1.setLayout(g1);

    }

    public void actionPerformed(ActionEvent e) {
        String st=e.getActionCommand();

        switch(st) {
            case "加密":
            {
                if(x1 && (jte.getText()).length()!=0) {
                    String str=jte.getText();
                    try {
                        File f = new File("D:/加密.txt");

                        char []a=str.toCharArray();
                        char secret=' ';
                        for(int i=0;i<a.length;i++)
                        {
                            char c=(char)(secret^a[i]);
                            ys+=c;
                        }
                        FileWriter fw = new FileWriter(f);
                        BufferedWriter bw = new BufferedWriter(fw);
                        fw.write(ys);

                        bw.close();
                        fw.close();

                    } catch (IOException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                    jbt3.setVisible(true);
                    jbt4.setVisible(true);
                    JOptionPane.showMessageDialog(jp1,"加密成功");
                    x1=false;
                    x2=true;
                }
                else if((jte.getText()).length()==0){
                    JOptionPane.showMessageDialog(jp1,"请输入待加密内容！");
                }
                else
                {
                    JOptionPane.showMessageDialog(jp1,"不能重复加密哦！");
                }
            }
            break;
            case "解密" :
                if(x2 && (jte.getText()).length()!=0){
                    {
                        String str=ys;
                        try {
                            File f = new File("D:/解密.txt");
                            char []a=str.toCharArray();
                            char secret=' ';
                            for(int i=0;i<a.length;i++) {
                                char c=(char)(secret^a[i]);
                                yc+=c;
                            }
                            FileWriter fw = new FileWriter(f);
                            BufferedWriter bw = new BufferedWriter(fw);
                            fw.write(yc);

                            bw.close();
                            fw.close();
                        } catch (IOException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                        JOptionPane.showMessageDialog(jp1,"解密成功！");
                        x2=false;
                    }

                }
                else if(x2==false &&yc.length()!=0){
                    JOptionPane.showMessageDialog(jp1,"已经解密了哦！");
                }
                else {
                    JOptionPane.showMessageDialog(jp1,"无密可解！");
                }
                break;
            case "查看加密文件":{
                jf2.setTitle("加密.txt");
                File f1=new File("D:/加密.txt");
                FileReader fr;
                try {
                    fr = new FileReader(f1);
                    BufferedReader br=new BufferedReader(fr);
                    jte1.setText(br.readLine());
                    br.close();
                    fr.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                jte1.setFont(ft);
                jf2.setVisible(true);

            }
            break;
            case "查看解密文件":{
                if(x2==false && yc.length()!=0) {
                    jf2.setTitle("解密.txt");
                    File f1=new File("D:/解密.txt");
                    FileReader fr;
                    try {
                        fr = new FileReader(f1);
                        BufferedReader bur=new BufferedReader(fr);
                        jte1.setText(bur.readLine());
                        bur.close();
                        fr.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    jte1.setFont(ft);
                    jf2.setVisible(true);
                }
                else if(yc.length()==0)
                    JOptionPane.showMessageDialog(jp1,"还未解密哦！");
            }

            break;
        }
    }



    public static void main(String args[]) {
        new Main();
    }
}